#include <stdio.h>
#include <stdlib.h>

#include "SDL.h"

static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;

void fill_audio(void *udata, Uint8 *stream, int len);

int main(int argc, char *argv[])
{

    /* Initialize SDL first */
      if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
          fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
          exit(1);
      }
      atexit(SDL_Quit);

      /* Find out how many CD-ROM drives are connected to the system */
      printf("Drives available: %d\n", SDL_CDNumDrives());
      int i;
      for ( i=0; i<SDL_CDNumDrives(); ++i ) {
          printf("Drive %d:  \"%s\"\n", i, SDL_CDName(i));
      }

    SDL_AudioSpec wanted;

    /* Set the audio format */
    wanted.freq = 22050;
    wanted.format = AUDIO_S16;
    wanted.channels = 2;    /* 1 = mono, 2 = stereo */
    wanted.samples = 1024;  /* Good low-latency value for callback */
    wanted.callback = fill_audio;
    wanted.userdata = NULL;

    /* Open the audio device, forcing the desired format */
    if ( SDL_OpenAudio(&wanted, NULL) < 0 ) {
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
        return(-1);
    }

    /* Load the audio data ... */

    ;;;;;

    audio_pos = audio_chunk;

    /* Let the callback function play the audio chunk */
    SDL_PauseAudio(0);

    /* Do some processing */

    ;;;;;

    /* Wait for sound to complete */
    while ( audio_len > 0 ) {
        SDL_Delay(100);         /* Sleep 1/10 second */
    }
    SDL_CloseAudio();

    return(0);
}

/* The audio function callback takes the following parameters:
     stream:  A pointer to the audio buffer to be filled
     len:     The length (in bytes) of the audio buffer
  */
void fill_audio(void *udata, Uint8 *stream, int len)
{
    /* Only play if we have data left */
    if ( audio_len == 0 )
        return;

    /* Mix as much data as possible */
    len = ( len > audio_len ? audio_len : len );
    SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    audio_pos += len;
    audio_len -= len;
}
